<?php

/**
 * @author nhatnam
 * @copyright 2011
 */
include ('clsketnoi.php');

function taomadocgia()
{
    return taomatudong("select madg from tbldocgia", "madg", 2, 10);
}

function uploadfile()
{
        
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    
    if (file_exists("../upload/hinhdocgia/" . $_FILES["file"]["name"]))
      {
      
      }
    else
      {
      move_uploaded_file($_FILES['file']['tmp_name'], "../upload/hinhdocgia/".$_FILES['file']['name']);
   
      }
    }
  }
else
  {
  
  }
}

//them du lieu
function themdocgia()
{
    $ngaysinh = $_POST['ngaysinh'];
    $madocgia= $_POST["madocgia"];
    $hoten =$_POST["hoten"];
    $loaidocgia = $_POST['loaidocgia'];
    $ngaylap = $_POST['ngaylapthe'];
    $tienno = $_POST['tienno'];
    //lay ten hinh
    $hinh = $_FILES["file"]["name"];

    $gioitinh = $_POST['gioitinh'];
    $diachi = $_POST['diachi'];
    $email = $_POST['email'];
    $manv = $_POST['manv'];

    $query = mysql_query("insert into tbldocgia (madg, manv, hinh, hoten, ngaysinh, gioitinh, loaidg, ngaylap, diachi, email, tienno)".
                " values('$madocgia','$manv','$hinh','$hoten','$ngaysinh','$gioitinh','$loaidocgia','$ngaylap','$diachi','$email','$tienno')",dbketnoi());
    mysql_free_result($query);
}
//xoa 
function xoadocgia()
{
    $madg= $_POST["madocgia"];
    $query = mysql_query("SELECT tbldocgia.madg as madg, mapm, mapttp, maphieughinhan from tbldocgia, tblghinhanmatsach, tblphieuthutienphat, tblphieumuon".
    " where tbldocgia.madg=tblghinhanmatsach.madg and tbldocgia.madg=tblphieuthutienphat.madg and tbldocgia.madg=tblphieumuon.madg",dbketnoi());
    $i=0;
    while($result = mysql_fetch_array($query))
    {
        if($result['madg']==$madg)
        {
            $q= mysql_query("delete from tblghinhanmatsach where maphieughinhan='".$result['maphieughinhan']."'", dbketnoi());
            mysql_free_result($q);
            $x=mysql_query("delete from tblphieuthutienphat where mapttp='".$result['mapttp']."'", dbketnoi());
            mysql_free_result($x);
            $y=mysql_query("delete from tblphieumuon  where mapm='".$result['mapm']."'", dbketnoi());
            mysql_free_result($y);
        }
        $i++;
    }
    
    $query = mysql_query("delete from tbldocgia where madg='$madg'",dbketnoi());
    mysql_free_result($query);
}
//update 
function updatedocgia()
{
    $ngaysinh = $_POST['ngaysinh'];
    $madocgia= $_POST["madocgia"];
    $hoten =$_POST["hoten"];
    $loaidocgia = $_POST['loaidocgia'];
    $ngaylap = $_POST['ngaylapthe'];
    $tienno = $_POST['tienno'];
    //lay ten hinh
    $hinh = $_FILES["file"]["name"];

    $gioitinh = $_POST['gioitinh'];
    $diachi = $_POST['diachi'];
    $email = $_POST['email'];
    $manv = $_POST['manv'];

    $query =mysql_query("update tbldocgia set manv='$manv', hinh='$hinh', hoten='$hoten', ngaysinh='$ngaysinh', gioitinh='$gioitinh', ".
    " loaidg='$loaidocgia', ngaylap='$ngaylap', diachi='$diachi', email='$email', tienno='$tienno' where madg='$madocgia' ",dbketnoi());

    mysql_free_result($query);
}

// hien thi du lieu
function hienthidocgia()
{
    $limit = 10; //gioi han so row trong 1 trang

    $page = isset ( $_GET["page"] ) ? intval ( $_GET["page"] ) : 1; //get trang hien thi

    $page_start = ( $page - 1 ) * $limit;
    $page_end = $page * $limit;

    $query = mysql_query("select madg, tbldocgia.hoten as hoten, tbldocgia.ngaysinh as ngaysinh, tbldocgia.gioitinh as gioitinh, tbldocgia.diachi as diachi, email, loaidg, tienno, ngaylap, tblnhanvien.hoten as tennhanvien ".
    "from tbldocgia, tblnhanvien where tblnhanvien.manv = tbldocgia.manv",dbketnoi());

    $number_of_page = ceil(mysql_num_rows($query)/$limit); // lay tong row

    if ($number_of_page > 1)
    {
        $list_page = " <td> Trang: </td>";

        for ( $i = 1; $i <= $number_of_page; $i++ )
            {
            if ( $i == $page )
            {
                $list_page .= " <td>[ <b>{$i}</b> ]</td> ";
           
            }
            else
            {
                $list_page .= "<td ><a href='giaodiendocgia.php?page={$i}'> {$i} </a></td>";
                
            }
        }
    }
    taotable();
    $i = 0;
    while ( $result = mysql_fetch_array ( $query ) )
    {
        if ( $i >= $page_start )
        {
            //mau bg cho dong chan le
            if($i%2==0)
                $bg = "#d0eafb";
            else
                $bg = "#66aee0";
            
            //tao ra 1 table bang lenh ket hop css
            echo "<tr bgcolor='".$bg."'>"; 
            echo "<td><a href='giaodiendocgia.php?madg=".$result['madg']."' >".$result['madg']."</a></td>";
            echo "<td>".$result['hoten']."</td>";
            echo "<td>".$result['gioitinh']."</td>";
            echo "<td>".$result['ngaysinh']."</td>";
            echo "<td>".$result['email']."</td>";
            echo "<td>".$result['diachi']."</td>";
            echo "<td>".$result['ngaylap']."</td>";
            echo "<td>".$result['tennhanvien']."</td>";
            echo "<td>".$result['loaidg']."</td>";
            echo "<td>".$result['tienno']."</td>";
            echo "</tr>";
        }

        $i++;

        if ($i >= $page_end)
        {
            break;
        }
    }
    echo "</table></br>";
    echo $list_page;
}
function taotable()
{
    $title = array("Mã ĐG ","Họ Tên","Giới Tính","Ngày Sinh", "Email", "Địa Chỉ", "Ngày Lập", "Tên Nhân Viên","Loại ĐG", "Tiền Nợ");
    echo "<table style='border-color:black;'  cellspacing='1' width='900px' >";
    echo "<tr>";
    for($i=0; $i<=9; $i++)
    {
        echo "<th style='background-color:#2287cd; color='white';>".$title[$i]."</th>"; 
    }
    echo "</tr>";
  
}
function hienthinhanvien()
{
    $query = mysql_query("select manv, hoten, bophan from tblnhanvien where bophan='Thủ kho'",dbketnoi());
    $i=0;
    echo "<select size='1' name='manv'>";
    echo "<option value=''>----Chọn----</option>";
    while($result = mysql_fetch_array($query) )
    {
        echo "<option value='".$result['manv']."'>".$result['hoten']."</option>";
        $i++;
    }
    echo "</select>";
}


function thongtindocgia()
{
    $madg = $_GET['madg'];
    $query = mysql_query("select madg, hoten, hinh, email from tbldocgia where madg='$madg'", dbketnoi());
    $i = 0;
    while ( $result = mysql_fetch_array ( $query ) )
    {
        
           echo "
            <table>
                <tr>
                    <td><img height='150' width='110' src='upload/hinhdocgia/".$result['hinh']."'/></td>
                </tr>
                <tr>
                    <td>Tên Độc Giả: ".$result['hoten']."</td>
                </tr>
                <tr>
                    <td>Email: ".$result['email']."</td>
                </tr>
            </table>
        
            </div>";
       
        $i++;
    }
   
}
?>
